package com.aplink.generic.util.objectrule;

import java.io.Serializable;
import java.util.ArrayList;

import android.content.pm.PackageManager.NameNotFoundException;

import com.aplink.generic.controller.GenericApplication;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.formdesign.PassingValue;

public class Target implements Serializable {
    private static final long serialVersionUID = 247657086117940598L;
    private String mPage;
    private PassingValue mPassingValue;
    private ArrayList<Rule> mRules;
    private String mType;

    public String getPage() {
        return this.mPage;
    }

    public Target getPage(final GenericObject genericObject) {
        if (this.mType.equalsIgnoreCase("AND")) {
            boolean result = true;
            for (final Rule rule : this.mRules) {
                result = result && rule.isValid(genericObject);
            }
            if (result) {
                return this;
            }
        } else {
            boolean result = false;
            for (final Rule rule : this.mRules) {
                result = result || rule.isValid(genericObject);
            }
            if (result) {
                return this;
            }
        }
        return null;
    }

    public PassingValue getPassingValue() {
        return this.mPassingValue;
    }

    public ArrayList<Rule> getRules() {
        return this.mRules;
    }

    public Class<?> getTargetClass() throws NameNotFoundException,
            ClassNotFoundException {
        return GenericApplication.getInstance().getTargetActivity(this.mPage);
    }

    public String getType() {
        return this.mType;
    }

    public void setPage(final String page) {
        this.mPage = page;
    }

    public void setPassingValue(final PassingValue passingValue) {
        this.mPassingValue = passingValue;
    }

    public void setRules(final ArrayList<Rule> rules) {
        this.mRules = rules;
    }

    public void setType(final String type) {
        this.mType = type;
    }
}
